<template>
  <el-dialog :visible.sync="visible"
             :title="!dataForm.id ? `${ $t('add')}角色权限` : `${$t('update')}角色权限`"
             :modal="false"
             class="user-dialog add-or-updata"
             width="35%"
             :before-close="closeDialog"
             :close-on-click-modal="false"
             :close-on-press-escape="false">
    <el-form :model="dataForm"
             class="label-left-form"
             :rules="dataRule"
             v-loading="dataListLoading"
             :element-loading-text="$t('loading')"
             label-position="top"
             ref="dataForm"
             @keyup.enter.native="dataFormSubmitHandle()"
             label-width="120px">
      <el-form-item prop="permissionList"
                    class="dept-list">
        <el-select filterable
                   v-model.trim="dataForm.permissionList"
                   multiple
                   collapse-tags
                   class="width-100"
                   :multiple-limit="!dataForm.id ? 0:1"
                   :placeholder="$t('rolePermission.permissionName')">
          <el-option v-for="item in permissionList"
                     :key="item.id"
                     :label="item.name"
                     :value="item.id"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button type="primary"
                 :loading="isRequest"
                 @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
      <el-button @click="closeDialog()">{{ $t('cancel') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import debounce from 'lodash/debounce'
export default {
  mixins: [mixinViewModule],
  props: {
    isAdd: Boolean
  },
  data () {
    return {
      dataListLoading: false,
      isRequest: false,
      visible: false,
      deptListVisible: false,
      permissionList: [],
      mixinViewModuleOptions: {
        getPermissionListURL: 'system/sysPermission/getPermission', // 获取权限列表接口
        getDataInfoURL: '/system/sysRolePermission/getById?id=', // 获取角色权限的接口
        addOrUpdateURL: '/system/sysRolePermission' // 添加或修改的接口
      },
      dataForm: {
        id: '',
        roleId: '',
        permissionList: []
      }
    }
  },
  computed: {
    dataRule () {
      return {
        roleId: [
          { required: true, message: this.$t('validate.required'), trigger: 'change' }
        ],
        permissionList: [
          { required: true, message: this.$t('validate.required'), trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    closeDialog () {
      this.visible = false
      this.isRequest = false
      this.$refs.dataForm.resetFields()
    },
    init (data) {
      this.dataForm.roleId = data.roleId
      this.dataForm.id = data.id
      this.visible = true
      // eslint-disable-next-line no-unneeded-ternary
      this.$nextTick(() => {
        this.getPermission()
        if (this.dataForm.id) {
          this.getDataInfo().then(res => {
            if (res.code !== 0) { return }
            this.dataForm.permissionList = []
            this.dataForm.permissionList.push(...res.data.permissionList)
          })
        }
      })
    },
    // 获取权限列表
    getPermission () {
      this.getDataList(this.mixinViewModuleOptions.getPermissionListURL, true, {
        roleId: this.dataForm.roleId,
        page: 1,
        pageSize: 500
      }).then(res => {
        if (res.code !== 0) { return }
        this.permissionList = res.data
      })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.isRequest = true
      this.addOrUpdateItem().then(res => {
        this.isRequest = false
        if (res.code !== 0) { return }
        this.closeDialog()
        this.$emit('callBack')
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>

<style lang="scss">
.user-dialog {
  background: rgba(0, 0, 0, 0.5);
}
.mod-sys__user {
  .role-list {
    .el-select {
      width: 100%;
    }
  }
}
</style>
